package com.logan.uc.exceptions;

import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class WebExceptionHandler {

    private Environment environment;

    public WebExceptionHandler(Environment environment) {
        this.environment = environment;
    }

    @ResponseBody
    @ExceptionHandler(value = UserCenterException.class)
    public ResponseEntity<Map> onUserCenterException(UserCenterException e) {
        Map<String, Object> result = new HashMap<>();
        result.put("message", e.getMessage());
        return new ResponseEntity<>(result, HttpStatus.SERVICE_UNAVAILABLE);
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<Map> onException(Exception e) {
        Map<String, Object> result = new HashMap<>();
        result.put("message", e.getMessage());
        return new ResponseEntity<>(result, HttpStatus.SERVICE_UNAVAILABLE);
    }
}
